package com.test.mytest.swt.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class DateComposite extends Composite {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
	private Date date = new Date();
	private Text txty;
	List<DateBean> list = new ArrayList<>();
	private Composite composite_date;
	private Composite composite;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public DateComposite(Composite parent, int style) {
		super(parent, style);
		GridData gd_composite_2 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_composite_2.widthHint = 196;
		this.setLayoutData(gd_composite_2);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginHeight = 0;
		gridLayout.horizontalSpacing = 0;
		setLayout(gridLayout);

		Composite composite_1 = new Composite(this, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(5, false);
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				date = TestDate.addDate(date, Calendar.YEAR, -1);
				refresh();
			}
		});
		btnNewButton.setText("<<");

		Button button = new Button(composite_1, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				date = TestDate.addDate(date, Calendar.MONTH, -1);
				refresh();
			}
		});
		button.setText("<");

		txty = new Text(composite_1, SWT.BORDER);
		txty.setText(sdf.format(date));
		txty.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		txty.setTextLimit(8);
		txty.addKeyListener(new KeyAdapter() {
			Timer t = null;

			@Override
			public void keyReleased(KeyEvent e) {
				if (t == null) {
					t = new Timer();
					t.schedule(new TimerTask() {
						@Override
						public void run() {
							getDisplay().asyncExec(new Runnable() {
								@Override
								public void run() {
									try {
										Date parse = sdf.parse(txty.getText());
										date = parse;
										refresh();
									} catch (ParseException e1) {
										e1.printStackTrace();
										txty.setText(sdf.format(date));
									} finally {
										t = null;
									}
								}
							});
						}
					}, 1000);
				}
			}
		});

		Button button_1 = new Button(composite_1, SWT.NONE);
		button_1.setText(">");

		Button button_2 = new Button(composite_1, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				date = TestDate.addDate(date, Calendar.YEAR, 1);
				refresh();
			}
		});
		button_2.setText(">>");

		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);

		Composite composite_3 = new Composite(composite, SWT.NONE);
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_composite_3.widthHint = 175;
		composite_3.setLayoutData(gd_composite_3);
		GridLayout gl_composite_3 = new GridLayout(7, false);
		gl_composite_3.horizontalSpacing = 15;
		gl_composite_3.marginWidth = 0;
		composite_3.setLayout(gl_composite_3);

		Label lblNewLabel = new Label(composite_3, SWT.NONE);
		GridData gd_lblNewLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblNewLabel.widthHint = 15;
		lblNewLabel.setLayoutData(gd_lblNewLabel);
		lblNewLabel.setText("一");

		Label label = new Label(composite_3, SWT.NONE);
		GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label.widthHint = 15;
		label.setLayoutData(gd_label);
		label.setText("二");

		Label label_1 = new Label(composite_3, SWT.NONE);
		GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_1.widthHint = 15;
		label_1.setLayoutData(gd_label_1);
		label_1.setText("三");

		Label label_2 = new Label(composite_3, SWT.NONE);
		GridData gd_label_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_2.widthHint = 15;
		label_2.setLayoutData(gd_label_2);
		label_2.setText("四");

		Label label_3 = new Label(composite_3, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_3.widthHint = 15;
		label_3.setLayoutData(gd_label_3);
		label_3.setText("五");

		Label label_4 = new Label(composite_3, SWT.NONE);
		GridData gd_label_4 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_4.widthHint = 15;
		label_4.setLayoutData(gd_label_4);
		label_4.setText("六");

		Label label_5 = new Label(composite_3, SWT.NONE);
		GridData gd_label_5 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_5.widthHint = 15;
		label_5.setLayoutData(gd_label_5);
		label_5.setText("日");
		createDate(composite);

		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				date = TestDate.addDate(date, Calendar.MONTH, 1);
				refresh();
			}
		});
		
		
	}

	/**
	 * 
	 */
	private void refresh() {
		createDate(composite);
		composite.layout();
	}

	private void createDate(Composite composite) {
		txty.setText(sdf.format(date));
		if (composite_date != null) {
			composite_date.dispose();
			composite_date = null;
		}
		composite_date = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_2 = new GridLayout(7, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.horizontalSpacing = 15;
		composite_date.setLayout(gl_composite_2);
		GridData gd_composite_2_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_2_1.heightHint = 133;
		composite_date.setLayoutData(gd_composite_2_1);

		list = TestDate.getListDateBean(date);
		for (DateBean dateBean : list) {
			Label label = new Label(composite_date, SWT.NONE);
			GridData gd_label_6 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
			gd_label_6.widthHint = 15;
			label.setLayoutData(gd_label_6);
			label.setText("" + dateBean.getDay());
			label.setData("time", dateBean.getDate());
			label.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseDoubleClick(MouseEvent e) {
					System.out.println(label.getData("time"));

				}
			});
			label.setAlignment(SWT.CENTER);
			if (dateBean.isNowMonth()) {
				label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
			}
			label.addMouseTrackListener(new MouseTrackListener() {
				@Override
				public void mouseHover(MouseEvent e) {
					label.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
				}

				@Override
				public void mouseExit(MouseEvent e) {
					if (dateBean.isNowMonth()) {
						label.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
					} else {
						label.setBackground(null);
					}
				}

				@Override
				public void mouseEnter(MouseEvent e) {
				}
			});
		}
	}

}
